<?php
namespace addons\yexam\service;

use app\admin\model\yexam\Question;
use app\admin\model\yexam\Unit;
use app\common\model\Category;

class Subject
{
    public $model;
    public function __construct()
    {
        $this->model = new \app\admin\model\yexam\Subject();
    }
    /**
     * 获取科目列表
     * @param $page
     * @param $limit
     * @throws \think\Exception
     */
    public function getSubjectList($page,$limit,$categoryId){
	$condition = ['status'=>1, 'category_id' => $categoryId];
        $count = $this->model->where($condition)->count();
        if($page){
            $data = $this->model->where($condition)->page("$page,$limit")->order("weigh asc")->select();
        }else{
            $data = $this->model->where($condition)->order("weigh asc")->select();
        }
        return ['total'=>$count,'data'=>$data];
    }
	public function getSubjectListKemu($page,$limit){
	$condition = ['status'=>1];
        $count = $this->model->where($condition)->count();
        if($page){
            $data = $this->model->where($condition)->page("$page,$limit")->order("id asc")->select();
        }else{
            $data = $this->model->where($condition)->order("id asc")->select();
        }
        return ['total'=>$count,'data'=>$data];
    }
	 public function getSubjectFenkemu($page,$limit,$categoryId,$isCategory=0){
        if ($isCategory == 0) {
            $condition['category_id'] = array('in',$categoryId);
            $condition['status'] = 1;
            $count = $this->model->where($condition)->count();
            $category = new Category();
            $categoryList = $category->where('id','in',$categoryId)->field('id,bname,type,flag')->order("weigh asc")->select();
            foreach ($categoryList as $key => $value)
            {
                $categoryList[$key]['category_bname'] = $value['bname'];
                $categoryList[$key]['is_show'] = false;
                $condition['category_id'] = $value['id'];
                $subjectList = $this->model->where($condition)->field('id,short_name,subject_code')->order("weigh asc")->select();
                foreach ($subjectList as $k => $v)
                {
                    $code = "[代码：{$v['subject_code']}]";
                    if ($v['subject_code'] == 0) $code = null;
                    $subjectList[$k]['short_name'] = $v['short_name'].$code;
                }
                $categoryList[$key]['subjects'] = $subjectList;
            }
            return ['total'=>$count,'data'=>$categoryList,'type' => 1];
        }else{
            //==== 科目顶级展示
            if ($isCategory == 3){
                $category =new Category();
                $topCategory = $category->where('id','in',$categoryId)->order("weigh","asc")->select();
                foreach ($topCategory as $key => $value)
                {
                    $result = $topCategory[$key]['second']= $category->where('pid',$value['id'])->select();
                    foreach ($result as $k => $v){
                        $result[$k]['third'] = $category->where('pid',$v['id'])->select();
                    }
                }
                //$topCategory = $this->model->where('category_id','in',$categoryId)->select();
                $count = count($topCategory);
                $data = $topCategory;
                return ['total' => $count,'data' => $data,'type' => 3];
            }else{
                if (!is_null($categoryId))
                    $condition['id'] = array('in',$categoryId);
                $condition['status'] = 1;
                $count = $this->model->where($condition)->count();
                if($page){
                    $data = $this->model->where($condition)->page("$page,$limit")->order("weigh asc")->select();
                } else{
                    $data = $this->model->where($condition)->order("id asc")->select();
                }
                foreach ($data as $key => $value)
                {
                    $code = "[代码：{$value['subject_code']}]";
                    if ($value['subject_code'] == 0) $code = null;
                    $data[$key]['short_name'] = $value['short_name'].$code;
                }
            }
            return ['total'=>$count,'data'=>$data,'type' => 0];
        }
    }
    /**
     * 获取当前科目下的章节
     */
    public function getUnitList($subject_id,$pid,$page,$limit){
        $model = new Unit();
        $count = $model->where(['status'=>1,'subject_id'=>$subject_id,'pid'=>$pid])->count();
        if($page){
            $data = $model->field("id,unit_name as name,is_last")->where(['status'=>1,'subject_id'=>$subject_id,'pid'=>$pid])->page($page,$limit)->order("sort asc")->select();
        }else{
            $data = $model->field("id,unit_name as name,is_last")->where(['status'=>1,'subject_id'=>$subject_id,'pid'=>$pid])->order("sort asc")->select();
        }

        return ['total'=>$count,'data'=>$data];
    }
    /**
     * 获取科目下是否有对应的免费章节或者免费的试题
     * @param $subjectId
     * @return int|string
     * @throws \think\Exception
     */
    public function getFreeUnit($subjectId)
    {
        $unit = new Unit();
        $result = $unit->where('is_free',0)->where('subject_id',$subjectId)->count();
        if ($result == 0) {
            //=========== 是否有免费的试题
            $question = new Question();
            return $question->where('subject_id',$subjectId)->where('is_free',0)->count();
        }
        else
            return $result;
    }
    /**
     * 获取指定科目下章节练习的题目总数
     */
    public function getSubjectQuestionNum($subject_id){
        $questionModel = new Question();
        return $questionModel->where(['subject_id'=>$subject_id,'unit_id'=>['neq',0]])->count();
    }

    /**
     * 获取指定科目下的章节练习的题目完成数量
     */
    public function getSubjectQuestionTestNum($subject_id,$user_id){
        $questionModel = new Question();
        return $questionModel
            ->alias("question")
            ->join("yexam_question_log log","question.id=log.question_id","inner")
            ->where(['question.subject_id'=>$subject_id,'question.unit_id'=>['neq',0],"log.user_id"=>$user_id])->count();
    }
    /**
     * 获取指定科目下的章节练习的题目正确数量
     */
    public function getSubjectQuestionRightNum($subject_id,$user_id){
        $questionModel = new Question();
        return $questionModel
            ->alias("question")
            ->join("yexam_question_log log","question.id=log.question_id","inner")
            ->where(['question.subject_id'=>$subject_id,'question.unit_id'=>['neq',0],"log.state"=>1,"log.user_id"=>$user_id])->count();
    }
}
